/*
 * Copyright � 2008 Nokia Corporation.
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <e32std.h>
#include <Sudoku_0xE4824C7F.rsg>
#include "Sudoku.hrh"
#include "Sudoku.pan"
#include "SudokuApplication.h"
#include "SudokuAppUi.h"
#include "SudokuAppView.h"
#include "GraphicEngine.h"
#include "SudokuGameOverDialog.h"
#include "SudokuStatisticControl.h"

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CSudokuAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CSudokuAppUi::ConstructL ( )
    {
    // Initialise app UI with standard value.
    BaseConstructL (CAknAppUi::EAknEnableSkin);

    // Hide status pane
    CEikStatusPane* pane = StatusPane();
    pane->MakeVisible(EFalse);

    // Hide CBA buttons
    CEikButtonGroupContainer* cba = Cba();
    if (cba)
        {
        cba->MakeVisible(EFalse);
        }

    SearchCbaPosition();
    SearchOptionsButtonPosition();
    
    iGraphicEngine = CGraphicEngine::NewL();
    
    // Create view object
    iAppView = CSudokuContainerControl::NewL(ClientRect( ));
    AddToStackL(iAppView);
    }
// -----------------------------------------------------------------------------
// CSudokuAppUi::CSudokuAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CSudokuAppUi::CSudokuAppUi ( )
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CSudokuAppUi::~CSudokuAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CSudokuAppUi::~CSudokuAppUi ( )
    {
    if (iAppView )
        {
        RemoveFromStack(iAppView);
        delete iAppView;
        
        delete iGraphicEngine;
        }

    }

// -----------------------------------------------------------------------------
// CSudokuAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CSudokuAppUi::HandleCommandL (TInt aCommand )
    {
    switch (aCommand )
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            {
            Exit ( );
            break;
            }
        default:
            {
            Panic (ESudokuUi );
            break;
            }
        };
    }


void CSudokuAppUi::SearchCbaPosition()
    {
    // Searching CBA locations
    CEikButtonGroupContainer* cba = Cba();
    if(cba)
        {
        CCoeControl* options = cba->ControlOrNull(EAknSoftkeyOptions);
        CCoeControl* select = cba->ControlOrNull(EAknSoftkeySelect);
        if (options)
            {
            if (options->Rect().iTl.iX > ApplicationRect().Width()/2)
                {
                iCbaPosition = AknLayoutUtils::EAknCbaLocationRight;
                }
            else
                {
                iCbaPosition = AknLayoutUtils::EAknCbaLocationBottom;
                }
            }
        else if (select)
            {
            if (select->Rect().iTl.iX > ApplicationRect().Width()/2)
                {
                iCbaPosition = AknLayoutUtils::EAknCbaLocationRight;
                }
            else
                {
                iCbaPosition = AknLayoutUtils::EAknCbaLocationBottom;
                }
            }
        }
    }

TBool CSudokuAppUi::IsOptionsButtonOnTop()
    {
    return iOptionButtonOnTop;
    }

void CSudokuAppUi::UseOptionsExitCbaL()
    {
    if (!CbaVisible(EAknSoftkeyExit))
        {
        CEikButtonGroupContainer* cba = Cba();
        if (cba)
            {
            cba->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_EXIT);
            if (iAppView)
                {
                iAppView->CbaSizeChanged();
                iAppView->DrawBackground();
                }
            }
        }
    }

void CSudokuAppUi::UseSelectBackCbaL()
    {
    if (!CbaVisible(EAknSoftkeyBack))
        {
        CEikButtonGroupContainer* cba = Cba();
        if (cba)
            {
            cba->SetCommandSetL(R_AVKON_SOFTKEYS_SELECT_BACK);
            if (iAppView)
                {
                iAppView->CbaSizeChanged();
                iAppView->DrawBackground();
                }
            }
        }
    }

TBool CSudokuAppUi::CbaVisible(TInt aCommandId)
    {
    TBool ret = EFalse;
    CEikButtonGroupContainer* cba = Cba();
    if (cba)
        {
        CCoeControl* control = cba->ControlOrNull(aCommandId);
        if (control)
            {
            ret = ETrue;
            }
        }
    return ret;
    }


void CSudokuAppUi::SearchOptionsButtonPosition()
    {
    iOptionButtonOnTop = EFalse;
    CEikButtonGroupContainer* cba = Cba();
    if(!cba)
        {
        return;
        }
    CCoeControl* options = cba->ControlOrNull(EAknSoftkeyOptions);
    CCoeControl* select = cba->ControlOrNull(EAknSoftkeySelect);
    CCoeControl* exit = cba->ControlOrNull(EAknSoftkeyExit);
    
    // NOTE: There should be EAknSoftkeyBack in the application because
    // we use R_AVKON_SOFTKEYS_SELECT_BACK, but it seems that there is EAknSoftkeyCancel
    CCoeControl* back = cba->ControlOrNull(EAknSoftkeyBack);
    CCoeControl* cancel = cba->ControlOrNull(EAknSoftkeyCancel);

    if (options)
        {
        if(exit )
            {
            if( options->Position().iY < exit->Position().iY )
                {
                iOptionButtonOnTop = ETrue;
                }
            }
        else if(back )
            {
            if( options->Position().iY < back->Position().iY )
                {
                iOptionButtonOnTop = ETrue;
                }
            }
        }
    else if (select)
        {
        if(back)
            {
            if( select->Position().iY < back->Position().iY )
                {
                iOptionButtonOnTop = ETrue;
                }
            }
        else if(cancel )
            {
            if( select->Position().iY < cancel->Position().iY )
                {
                iOptionButtonOnTop = ETrue;
                }
            }
        }
    }

void CSudokuAppUi::HandleResourceChangeL( TInt aType )
    {
    CAknAppUi::HandleResourceChangeL( aType );
       
    if ( aType==KEikDynamicLayoutVariantSwitch )
        {
        SearchCbaPosition();
        SearchOptionsButtonPosition();
        if (iGameOverDlg)
            {
            iGameOverDlg->HandleResourceChange(aType);
            }
        }   
    }

void CSudokuAppUi::ShowGameOverL()
    {
    if (!iGameOverDlg)
        {
        iGameOverDlg = new (ELeave) CSudokuGameOverDialog();
        iGameOverDlg->ExecuteLD(R_GAMEOVER_DIALOG);
        iGameOverDlg = NULL;
        }
    }

// End of File
